<?php

/**
 * Created by PhpStorm
 * User Niu Yong Qiang
 * Date 2021/9/17 19:20
 */

namespace App\Services\Web;

use App\Enum\enum_wechat;
use App\Http\Format\FormatBase;
use App\Models\SysMessageModel;
use App\Models\SysPermissionsModel;
use App\Models\SysRoleModel;
use App\Models\SysRolePermissionsModel;
use App\Models\SysUserModel;
use App\Models\TableItemModel;
use App\Models\TableModel;
use App\Models\UserPublicModel;
use App\Models\UserPublicSearchModel;
use App\Models\UserTokenModel;
use App\Models\SysUserRoleModel;
use App\Services\CommonService;
use App\Services\CurlBasiceService;
use Exception;

class TaskService extends CommonService
{
    public function handleTask($params, $type = 1):array
    {
        if ($type == 1){
            //'day_type', 'time_type', 'cat_1', 'no', 'mobile', 'number', 'area', 'city', 'address']);
            //day_type 1今天 2昨天 3前天
            //time_type 1上午 2下午
            $params['day_type'] = $params['day_type'] ?? 1;
            $params['time_type'] = $params['time_type'] ?? 1;

            $data = $this->handleTaskDay($params);

        }else{
            $data = [];
        }

        if (!empty($params['style'])){
            $data[] = ['info_user_public.style', '=', $params['style']];
        }

        if (!empty($params['status'])){
            $data[] = ['info_admin_work.status', '=', $params['status']];
        }

        if (!empty($params['visit_status'])){
            if ($params['visit_status']  == 2){
                //已回访
                $data[] = ['info_user_public.visit_time' ,'>', 0];
            }
        }

        if (!empty($params['cat_1'])){
            $data[] = ['info_user_public.cat_1', '=', $params['cat_1']];
        }else if(isset($params['cat_1']) && $params['cat_1'] == 0){
            $data[] = ['info_user_public.status', '=', 1000];
        }

        if (!empty($params['cat_2'])){
            $data[] = ['info_user_public.cat_2', '=', $params['cat_2']];
        }

        if (!empty($params['no'])){
            $data[] = ['info_user_public.no', '=', $params['no']];
        }

        if (!empty($params['no'])){
            $data[] = ['info_user_public.no' ,'=', $params['no']];
        }

        if (!empty($params['area'])){
            $data[] = ['info_user_public.area' ,'=', $params['area']];
        }

        if (!empty($params['city'])){
            $data[] = ['info_user_public.city' ,'=', $params['city']];
        }

        if (!empty($params['address'])){
            $data[] = ['info_user_public.address' ,'=', $params['address']];
        }

        if (!empty($params['mobile'])){
            $data[] = ['info_user.mobile' ,'=', $params['mobile']];
        }

        if (!empty($params['number'])){
            $data[] = ['info_user.number' ,'=', $params['number']];
        }

        return $data;
    }

    public function handleTaskSearch($params, $type = 1):array
    {
        if ($type == 1){
            //'day_type', 'time_type', 'cat_1', 'no', 'mobile', 'number', 'area', 'city', 'address']);
            //day_type 1今天 2昨天 3前天
            //time_type 1上午 2下午
            $params['day_type'] = $params['day_type'] ?? 1;
            $params['time_type'] = $params['time_type'] ?? 1;

            $data = $this->handleTaskDaySearch($params);

        }else{
            $data = [];
        }

        if (!empty($params['style'])){
            $data[] = ['info_user_public_search.style', '=', $params['style']];
        }

        if (!empty($params['status'])){
            $data[] = ['info_admin_work.status', '=', $params['status']];
        }

        if (!empty($params['visit_status'])){
            $data[] = ['info_user_public_search.visit_status', '=', $params['visit_status']];
        }

        if (!empty($params['cat_1'])){
            $data[] = ['info_user_public_search.seach_cat_1', '=', $params['cat_1']];
        }else if(isset($params['cat_1']) && $params['cat_1'] == 0){
            $data[] = ['info_user_public_search.status', '=', 3];
        }

        if (!empty($params['cat_2'])){
            $data[] = ['info_user_public_search.cat_2', '=', $params['cat_2']];
        }

        if (!empty($params['no'])){
            $data[] = ['info_user_public_search.no','=', $params['no']];
        }

        if (!empty($params['area'])){
            $data[] = ['info_user_public_search.area' ,'=', $params['area']];
        }

        if (!empty($params['city'])){
            $data[] = ['info_user_public_search.city' ,'=', $params['city']];
        }

        if (!empty($params['address'])){
            $data[] = ['info_user_public_search.address' ,'=', $params['address']];
        }

        if (!empty($params['mobile'])){
            $data[] = ['info_user.mobile' ,'=', $params['mobile']];
        }

        if (!empty($params['number'])){
            $data[] = ['info_user.number' ,'=', $params['number']];
        }

        if (!empty($params['admin_id'])){
            $data[] = ['info_user_public_search.admin_id' ,'=', $params['admin_id']];
        }

        return $data;
    }


    public function handleData($params):array
    {
        //'style', 'cat_1', 'mobile', 'number', 'area', 'city', 'address', 'limit', 'page'
        //            , 'admin_id', 'is_pay', 'created_at_start', 'created_at_end'

        $data = [];
        if (!empty($params['no'])){
            $data[] = ['info_user_public.no', '=', $params['no']];
        }


        if (!empty($params['created_at_start'])){
            $data[] = ['info_user_public.created_at' ,'>=', strtotime($params['created_at_start'])];

        }

        if (!empty($params['created_at_end'])){
            $data[] = ['info_user_public.created_at' ,'<=', strtotime($params['created_at_end'])];

        }

        if (!empty($params['style'])){
            $data[] = ['info_user_public.style', '=', $params['style']];
        }

        if (!empty($params['cat_1'])){
            $data[] = ['info_user_public.cat_1', '=', $params['cat_1']];
        }

        if (!empty($params['area'])){
            $data[] = ['info_user_public.area' ,'=', $params['area']];
        }

        if (!empty($params['city'])){
            $data[] = ['info_user_public.city' ,'=', $params['city']];
        }

        if (!empty($params['address'])){
            $data[] = ['info_user_public.address' ,'=', $params['address']];
        }

        if (!empty($params['mobile'])){
            $data[] = ['info_user.mobile' ,'=', $params['mobile']];
        }

        if (!empty($params['number'])){
            $data[] = ['info_user.number' ,'=', $params['number']];
        }

        if (!empty($params['admin_id'])){
            $data[] = ['info_user_public.admin_id' ,'=', $params['admin_id']];
        }

        if (!empty($params['is_pay'])){
            $data[] = ['info_user_public.is_pay' ,'=', $params['is_pay']];
        }


        return $data;
    }

    public function handleTaskDay($params):array
    {
        $data = [];
        switch ($params['day_type']){
            case 1:
                $day = date('Y-m-d', time());
                $data = $this->handleTaskTime($day, $params);

                break;
            case 2:
                $day = date('Y-m-d', time()-86400);
                $data = $this->handleTaskTime($day, $params);

                break;
            case 3:
                $day = date('Y-m-d', time()-86400-86400);
                $data = $this->handleTaskTime($day, $params);

                break;
            default:
                $day = date('Y-m-d', time()-(86400*($params['day_type']-1)));
                $data = $this->handleTaskTime($day, $params);
                break;
        }

        return $data;
    }

    public function handleTaskTime($day, $params):array
    {
        $data = [];
        if (empty($params['time_type'])){
            $data[] = ['info_user_public.handle_time' ,'>=', strtotime($day . ' 00:00:00')];
            $data[] = ['info_user_public.handle_time' ,'<=', strtotime($day . ' 23:59:59')];

        }elseif ($params['time_type'] == 1){
            $data[] = ['info_user_public.handle_time' ,'>=', strtotime($day . ' 00:00:00')];
            $data[] = ['info_user_public.handle_time' ,'<=', strtotime($day . ' 12:00:00')];

        }elseif ($params['time_type'] == 2){
            $data[] = ['info_user_public.handle_time' ,'<=', strtotime($day . ' 23:59:59')];
            $data[] = ['info_user_public.handle_time' ,'>=', strtotime($day . ' 12:00:00')];
        }

        return $data;
    }

    public function handleTaskDaySearch($params):array
    {
        $data = [];
        switch ($params['day_type']){
            case 1:
                $day = date('Y-m-d', time());
                $data = $this->handleTaskTimeSearch($day, $params);

                break;
            case 2:
                $day = date('Y-m-d', time()-86400);
                $data = $this->handleTaskTimeSearch($day, $params);

                break;
            case 3:
                $day = date('Y-m-d', time()-86400-86400);
                $data = $this->handleTaskTimeSearch($day, $params);

                break;
            default:
                $day = date('Y-m-d', time()-(86400*($params['day_type']-1)));
                $data = $this->handleTaskTimeSearch($day, $params);
                break;
        }

        return $data;
    }

    public function handleTaskTimeSearch($day, $params):array
    {
        $data = [];
        if (empty($params['time_type'])){
            $data[] = ['info_user_public_search.handle_time' ,'>=', strtotime($day . ' 00:00:00')];
            $data[] = ['info_user_public_search.handle_time' ,'<=', strtotime($day . ' 23:59:59')];

        }elseif ($params['time_type'] == 1){
            $data[] = ['info_user_public_search.handle_time' ,'>=', strtotime($day . ' 00:00:00')];
            $data[] = ['info_user_public_search.handle_time' ,'<=', strtotime($day . ' 12:00:00')];

        }elseif ($params['time_type'] == 2){
            $data[] = ['info_user_public_search.handle_time' ,'<=', strtotime($day . ' 23:59:59')];
            $data[] = ['info_user_public_search.handle_time' ,'>=', strtotime($day . ' 12:00:00')];
        }

        return $data;
    }


    //imagecopyresampled函数将一个图像缩放到指定大小，并复制到另一个图像上
    public function image_add_image($backgroundImagePath, $imagePath, $sourceImage){
        // 提前准备好的海报图  必须是PNG格式
        $bg = imagecreatefrompng(public_path($backgroundImagePath));
        // 获取背景图片的宽度和高度
        $backgroundWidth = imagesx($bg);
        $backgroundHeight = imagesy($bg);
        //二维码
        $qrcodes = imagecreatefrompng(public_path($imagePath)); //二维码

        $x = $backgroundWidth - 250;
        $y = $backgroundHeight - 280;
        imagecopyresampled($bg, $qrcodes, $x, $y, 0, 0, imagesx($qrcodes), imagesx($qrcodes), imagesx($qrcodes), imagesx($qrcodes));
        imagepng($bg, $imagePath); //合并图片

        // 释放图像资源
//        imagedestroy($sourceImage);
    }

    //图片中添加文字
    public function addTextToBackgroundImage($backgroundImagePath, $text, $outputPath='path/img.png',$textAlignment='')
    {
        // 打开背景图片
        $backgroundImage = imagecreatefrompng(public_path($backgroundImagePath));

        // 获取背景图片的宽度和高度
        $backgroundWidth = imagesx($backgroundImage);
        $backgroundHeight = imagesy($backgroundImage);
        // 设置文字样式
        $font = public_path('css/GB2312.ttf'); // 替换为你的字体文件路径
        $fontSize = 35; // 字体大小
        $color = imagecolorallocate($backgroundImage, 255, 255, 255); // 文字颜色，这里设置为白色
        $positionX = 0; // 文字起始位置的 X 坐标
        $positionY = 0; // 文字起始位置的 Y 坐标
        // 获取文字的宽度和高度
        $textWidth = imagettfbbox($fontSize, 0, $font, $text);
        $textHeight = imagettfbbox($fontSize, 0, $font, $text);
        $positionX = ($backgroundWidth - $textWidth[2]) / 2;
        // 根据对齐方式计算文字的位置
        switch ($textAlignment) {
            case 'center':
                $positionX = ($backgroundWidth - $textWidth[2]) / 2;
                $positionY = ($backgroundHeight - $textHeight[1]) / 2;
                break;
            case 'top':
                $positionX = 50;
                $positionY = 100;
                break;
            case 'left':
                $positionX = $backgroundWidth - $textWidth;
                $positionY = 100;
                break;
            case 'bottom':
                $positionX = 50;
                $positionY = $backgroundHeight - $textHeight[1] - 100;
                break;
            default:
                // 默认居中
                $positionX = ($backgroundWidth - $textWidth[2]) / 2;
                $positionY = ($backgroundHeight - $textHeight[1]) / 2;
                break;
        }
        // 在背景图片上添加文字
        imagettftext($backgroundImage, $fontSize, 0, $positionX, $positionY, $color, $font, $text);

        // 输出最终图像到文件或浏览器
        if ($outputPath !== null) {
            imagepng($backgroundImage, $outputPath); // 输出为 PNG 格式的文件
        } else {
            header('Content-Type: image/png'); // 输出到浏览器时设置正确的图像类型头信息
            //imagepng($backgroundImage); // 输出为 PNG 格式的图像到浏览器
            //保存至本地文件
            imagepng($backgroundImage, $outputPath);
        }
        // 清理内存中的图像资源
        imagedestroy($backgroundImage);
    }

    public function handle_rpc_info($task_id, $s = 0)
    {
        if (!empty($s)){
            //获取任务
            $userPublic = UserPublicSearchModel::getSingle(['id'=> $task_id]);
        }else{
            //获取任务
            $userPublic = UserPublicModel::getSingle(['id'=> $task_id]);
        }

        $result_string = '';

        if(!empty($userPublic['create_title'])){
            $result_string .= '
标题：' . $userPublic['create_title'];
        }

        if(!empty($userPublic['create_lxr_name'])){
            $result_string .= '
联系人称呼：' . $userPublic['create_lxr_name'];
        }

        if(!empty($userPublic['create_lxr_mobile'])){
            $result_string .= '
联系电话：' . $userPublic['create_lxr_mobile'];
        }

        if (!empty($userPublic['rpc_id'])){
            $rpc_id = $userPublic['rpc_id'];
                //获取用户提交记录数据
            $result = CurlBasiceService::getInstance()->httpPost('http://api.customer.form.bailingkeji.com/admin/record/detail'
                , json_encode([
                    'filling_id'=> $rpc_id
                ]), []);

            if ($result['code'] == 0 && !empty($result['data']['record'])){
                foreach ($result['data']['record'] as $value){
                    switch ($value['bank_info']['type']){
                        case 1:
                            $result_string .= '
' . $value['bank_info']['name'] . '：' . $value['content'];

                            break;
                        case 2:
                            //单选
                            $result_string .= '
' . $value['bank_info']['name'] . '：' . $value['content'];

                            break;
                    }
                }
            }
        }


        return trim($result_string, '
');
    }
}
